﻿var currentItem;
var children = new Array();
var parents = new Array();
var Cookie_SELECTED_PATH = 'selectedPath';

function WebForm1OnPageClose() {

}

function WebForm1OnPageLoad() {
    var cookieValue =  cookieValueForName(Cookie_SELECTED_PATH);
    executeMenuItemId(cookieValue);
}

function clearMenuHeaderItems (elementId) {
    var menuHeader = document.getElementById(elementId);
    var child = null;
    var count = menuHeader.childNodes.length;
    var i;
    for (i = count - 1; i >= 0; i--) {
        child = menuHeader.childNodes.item(i);
        menuHeader.removeChild(child);
    }
    
}
function executeMenuItem(anItem) {
    var uniq = anItem.id;
    executeMenuItemId(uniq);
}

function executeMenuItemId(itemPath) {
    currentItem = (itemPath == undefined)?'':itemPath;

    createCookie(Cookie_SELECTED_PATH, currentItem, 365, '/');

    children.length = 0;
    parents.length = 0;

    executeService(
        '/services/ChromeBookmarksService.asmx/bookmarkParentListForItemPath',
        '{\'itemPath\':\'' + currentItem + '\'}',
        webForm1OnParentsSuccess,
        webForm1OnParentsError);
}

function drawChildrenDIVs(result, style) {
    var resultArray = new Array();
    var arr = $(result.d);
    for (var i = 0; i < arr.length; i++) {
        var elem = arr[i];
        if (elem != null) {
            var div = document.createElement("div");
            div.setAttribute("class", style);
            div.setAttribute("id", elem.unid);
            div.setAttribute("onClick", "executeMenuItem(this)");
            var text = document.createTextNode(elem.name);
            div.appendChild(text);
            resultArray.push(div);
        }
    }
    return resultArray; 
}

function webForm1OnParentsSuccess(result) {
    //var itemPath = (currentItem == undefined) ? '' : currentItem;
    parents = drawChildrenDIVs(result, "headerMenuItemParent");

    executeService(
        '/services/ChromeBookmarksService.asmx/bookmarkChildListForItemPath',
        '{\'itemPath\':\'' + currentItem + '\'}',
        webForm1OnChildrenSuccess,
        webForm1OnChildrenError);
}

function webForm1OnParentsError(xmlHttpRequest, status, err) {
    alert(xmlHttpRequest.statusText + " " + xmlHttpRequest.status + " : " + xmlHttpRequest.responseText);
}

function drawItemsTree(element, index, array) {
    var menuHeader = document.getElementById("treeNavigator");
    menuHeader.appendChild(element);

}

function drawItemsIcon(element, index, array) {
    var menuHeader = document.getElementById("iconNavigator");
    menuHeader.appendChild(element);

}

function webForm1OnChildrenSuccess(result) {

    children = drawChildrenDIVs(result, "headerMenuItemChild");

    clearMenuHeaderItems("treeNavigator");
    clearMenuHeaderItems("iconNavigator");
    parents.forEach(drawItemsTree);
    children.forEach(drawItemsIcon);
}

function webForm1OnChildrenError(xmlHttpRequest, status, err) {
    alert(xmlHttpRequest.statusText + " " + xmlHttpRequest.status + " : " + xmlHttpRequest.responseText);
}

